package sim.ring.gui.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Display;

import sim.ring.gui.RingingSimApp;
import sim.ring.gui.RingingSimManager;
import sim.ring.gui.databinding.MethodData;
import sim.ring.gui.dialogs.AddNewMethodDialog;
import sim.ring.gui.treedata.MethodTreeContentProvider;
import sim.ring.gui.treedata.TMethod;

public class AddNewMethodAction extends Action {
	
	private static String _CREATEERRORTITLE, _CREATEERRORMESSAGE;
	
	RingingSimApp r;
	
	public AddNewMethodAction(RingingSimApp parent) {
		r = parent;
		setText(r.getLocalisedData("newMethodAction", "&New method@Ctrl++"));
		setToolTipText(r.getLocalisedData("newMethodActionTooltip", "Create a new method"));
		setImageDescriptor(RingingSimManager.getIMGReg().getDescriptor("newMethod"));
		_CREATEERRORMESSAGE = r.getLocalisedData("methCreateErrorMessage", "Method could not be created. Details: %s");
		_CREATEERRORTITLE = r.getLocalisedData("methCreateErrorTitle", "Method creation error");
	}
	
	@Override
	public void run() {
		MethodData methodData = new MethodData();
		AddNewMethodDialog dlg = new AddNewMethodDialog(Display.getCurrent().getActiveShell(), methodData);
		if (dlg.open() == Window.OK) {
			TMethod newMethod = null;
			try {
				newMethod = new TMethod(methodData.getName(),
						methodData.getClassification(),
						methodData.getStage(),
						methodData.getNotation());
				((MethodTreeContentProvider)r.getTV().getContentProvider()).addNewMethod(newMethod);
			}
			catch (Exception exc) {
				MessageDialog.openError(Display.getCurrent().getActiveShell(),
						_CREATEERRORTITLE, String.format(_CREATEERRORMESSAGE, exc.getLocalizedMessage()));
			}
		}
	}

}
